package com.item.zk.controller;

import com.item.zk.lock.DistributedLockByCurator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.CountDownLatch;

/**
 * @author wd
 * @Title:
 * @Package
 * @Description:
 * @date 2020/5/24 18:57
 */
@RestController
public class LockController {


    @Autowired
    DistributedLockByCurator distributedLockByCurator;

    private final static String PATH = "test";


    public static void main(String[] args) {
        final CountDownLatch countDownLatch=new CountDownLatch(1);
        for (int i=0;i<10;i++){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        countDownLatch.await();
                    }catch (Exception e){

                    }
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss|SSS");
                    String orderNo=simpleDateFormat.format(new Date());
                    System.out.println("生成得订单号为："+orderNo);
                }
            }).start();
        }
        countDownLatch.countDown();
    }

    @RequestMapping("/")
    public  void noLock(){
        for (int i=0;i<10;i++){
            distributedLockByCurator.acquireDistributedLock(PATH);
            new Thread(new Runnable() {
                @Override
                public void run() {
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss|SSS");
                        String orderNo=simpleDateFormat.format(new Date());
                        System.out.println("生成得订单号为："+orderNo);
                }
            }).start();
            distributedLockByCurator.releaseDistributedLock(PATH);
        }


    }

}
